<template>
  <a-modal
    :title="title"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
    width="600px"
    v-drag
  >
    <template slot="footer">
      <a-button @click="handleCancel" :disabled="confirmLoading">{{ $t('取消') }}</a-button>
      <a-button type="primary" @click="handleOk" :disabled="confirmLoading">{{ $t('确定') }}</a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-row :gutter="10">
        <a-col :span="24" style="margin-top:10px;">
          <span>{{ $t('投币类型') }}：</span>
          <a-select :placeholder="$t('请选择')" v-model="inputType" style="width:200px" @change="changeInputType">
            <a-select-option v-for="(type) in typeList" :key="type.value" :value="type.value">{{type.title}}</a-select-option>
          </a-select>
        </a-col>
        <a-col :span="24" style="margin-top:10px;" v-if="inputType != 0">
          <span>{{ $t('投币比例') }}：</span>
          <a-input-number v-model="rate" style="width:200px" :min="1" :precision="0"/> {{ inputValue }} = 1{{ $t('币') }}
        </a-col>
      </a-row>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { initDictOptions } from '@/components/dict/JDictSelectUtil'

export default {
  name: 'InputTypeModal',
  components: {},
  data() {
    return {
      title: this.$t('投币类型'),
      confirmLoading: false,
      visible: false,
      typeList: [],
      inputType: '',
      inputValue: '',
      rate: "1",
      dataSource: []
    }
  },
  mounted() {},
  methods: {
    async show(obj) {
      await this.getTypeList()
      this.inputType = ''
      this.inputValue = ''
      this.rate = "1"
      this.dataSource = obj.inputList
      this.visible = true
    },
    getTypeList(){
      return new Promise(resolve=>{
        initDictOptions('bis_game.input_type').then(res=>{
          if(res.success){
            this.typeList = res.result
          }else{
            this.typeList = []
          }
          resolve()
        })
      })
    },
    changeInputType(e){
      let y = this.typeList.findIndex(t => {
        return t.value == this.inputType
      })
      this.inputValue = this.typeList[y].title
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      if(this.inputType == ''){
        this.$message.warning(this.$t('请输入投币类型'))
        return
      }
      if(this.inputType != '0' && this.rate == ''){
        this.$message.warning(this.$t('请输入投币比例'))
        return
      }
      let x = this.dataSource.findIndex(d => {
        return d.type == this.inputType
      })
      let y = this.typeList.findIndex(t => {
        return t.value == this.inputType
      })
      if(x > -1){
        this.$message.warning(this.typeList[y].title+" "+this.$t('已存在'))
        return
      }
      this.$emit('ok',{type:this.inputType, type_dictText:this.typeList[y].title, rate:this.rate })
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>
<style scoped>
.bold-font {
  font-weight: bold;
}
</style>